package com.example.firstboot.util;

import com.example.firstboot.entity.Mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@RestController
public class MailUtils {

    @Autowired
    private  Mail mail;

    @RequestMapping("mail")
    public int test(){
        try {
                int code = sendVerificationCode("279524727@qq.com");
        }catch(Exception e) {
            e.printStackTrace();
        }
        return 123;
    }

    //声明属性对象
    /**
     * 发送邮件
     * @param to
     * @param title
     * @param content
     * @throws Exception
     */

    @RequestMapping("/mail2")
    public  void send(String to,String title,String content) throws Exception{
        //生成系统属性对象
        Properties prop = new Properties();
        prop.setProperty("mail.host", mail.getHost());
        prop.setProperty("mail.transport.protocol", mail.getProtocol());
        prop.setProperty("mail.smtp.auth", mail.getAuth());
        Session session = Session.getInstance(prop);
        session.setDebug(true);
        //获取Transport对象
        Transport ts = session.getTransport();
        //连接邮箱服务器
        ts.connect("smtp.qq.com",mail.getFrom(),mail.getCode());
        Message message = createSimpleMail(session,to,title,content);
        ts.sendMessage(message, message.getAllRecipients());
    }

    /**
     * 发送简单邮件（不包括图片和附件）
     * @param session
     * @param to
     * @return
     * @throws Exception
     */
    public MimeMessage createSimpleMail(Session session, String to, String title, String content) throws Exception{
        MimeMessage message = new MimeMessage(session);
        //设置发送邮箱地址
        message.setFrom(new InternetAddress(mail.getFrom()));
        //设置目标邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        //设置标题
        message.setSubject(title);
        //设置邮件内容
        message.setContent(content, "text/html;charset=UTF-8");
        return message;
    }

    /**
     * 发送验证码
     * @param to
     * @return
     * @throws Exception
     */
    public int sendVerificationCode(String to) throws Exception {
        //邮件标题
        String title = "医院就诊综合系统";
        //生成6位随机数
        int code = (int)((Math.random()*9+1)*100000);
        //邮件内容
        String content = "尊敬的用户：您本次的验证码为："+code+"，请尽快完成操作";
        send(to,title,content);
        return code;
    }

    /**
     * 重置密码
     * @param to
     * @return
     * @throws Exception
     */
    public Integer sendPassword(String to) throws Exception{
        //邮件标题
        String title = "医院就诊综合系统";
        //生成6位随机数
        int password = (int)((Math.random()*9+1)*100000);
        //邮件内容
        String content = "尊敬的用户：您密码已重置为："+password+"，请尽快修改密码！";
        //发送邮件
        send(to,title,content);
        return password;
    }

    public static void main(String[] args) {


    }
}
